package com.clouds.wms.cms;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.clouds.wms.bean.Cmsmd;
import com.clouds.wms.bean.Cmsmh;
import com.clouds.wms.bean.Cmsmk;
import com.clouds.wms.exception.SysException;
import com.clouds.wms.service.CmsmhService;
import com.clouds.wms.service.CmsmkService;
import com.clouds.wms.util.Function;
import com.clouds.wms.util.PagesBean;
import com.clouds.wms.util.RequestHelper;
import com.clouds.wms.util.SysParams;

@Controller
@RequestMapping("/cmsmh")
public class CmsmhController {

	private Log log = LogFactory.getLog(getClass());
	
	@Resource(name="cmsmhService")
	private CmsmhService cmsmhService;

	@Resource(name="cmsmkService")
	private CmsmkService cmsmkService;


	/**
	 * 仓库管理
	 * @author dz.wang
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmhMain.xhtml")
	public String cmsmhMain(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		
		return "cms/cmsmh";
	}
	
	/**
	 * 列出所有的仓库
	 * @param request
	 * @param response
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/cmsmhList.xhtml")
	public String cmsmhList(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		String mhab = RequestHelper.getString(request, "mhab");
		int mhad = RequestHelper.getInt(request, "mhad", 0);
		int pageNumber = RequestHelper.getInt(request, "page", 1);// 要跳转的页号
		int pageSize = RequestHelper.getInt(request, "rows", 10);// 一页的条数
		PagesBean pb = cmsmhService.getCmsmhList(mhaa, mhab,mhad, pageNumber, pageSize);
		List<Cmsmh> list = ((List<Cmsmh>)pb.getData());
		int count = pb.getCount();
		Map<String,Object> paraMap = new HashMap<String,Object>();
		paraMap.put("total", count);
		paraMap.put("rows", list);
		try {
			Function.toJson(response, paraMap);
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 添加仓库
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/saveCmsmh.xhtml", method = RequestMethod.POST)
	public String saveCmsmh(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmh cmsmh) throws SysException, IOException {
		String mhaa = cmsmh.getMhaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mhaa == null || "".equals(mhaa.trim())) {
			out.print("仓库编号不能为空!");
			return null;
		}
		Cmsmh cmsmatmp = cmsmhService.getCmsmhByMhaa(mhaa);
		if(cmsmatmp != null && cmsmatmp.getMhaa() != null && !"".equals(cmsmatmp.getMhaa())) {
			out.print("仓库编号已经存在!");
			return null;
		}
		cmsmhService.saveCmsmh(cmsmh);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("增加了编号为[" + mhaa + "]的仓库!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
	/**
	 * 仓库编辑
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/updateCmsmh.xhtml", method = RequestMethod.POST)
	public String updateCmsmh(HttpServletRequest request, HttpServletResponse response, Model model, Cmsmh cmsmh) throws SysException, IOException {
		String mhaa = cmsmh.getMhaa();
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mhaa == null || "".equals(mhaa.trim())) {
			out.print("仓库编号不能为空!");
			return null;
		}
		cmsmhService.updateCmsmh(cmsmh);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("修改了编号为[" + mhaa + "]的仓库!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}
	
	/**
	 * 禁用仓库
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/disableCmsmh.xhtml", method = RequestMethod.POST)
	public String disableCmsmh(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mhaa==null || mhaa.equals("")){
			out.print("仓库编号不能为空!");
			return null;
		}
		cmsmhService.updateCmsmhMham(mhaa, SysParams.STATUS_DISABLE);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("禁用了编号为[" + mhaa + "]的仓库!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 禁用仓库
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 * @throws IOException 
	 */
	@RequestMapping(value = "/startUsingCmsmh.xhtml", method = RequestMethod.POST)
	public String startUsingCmsmh(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException, IOException {
		String mhaa = RequestHelper.getString(request, "mhaa");
		Cmsmd globalUmUser = (Cmsmd)request.getSession().getAttribute("globalUmUser");
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = response.getWriter();
		if(mhaa==null || mhaa.equals("")){
			out.print("仓库编号不能为空!");
			return null;
		}
		cmsmhService.updateCmsmhMham(mhaa, SysParams.STATUS_NORMAL);
		Cmsmk cmsmk = new Cmsmk();
		cmsmk.setMkab(String.valueOf(globalUmUser.getMdaa()));
		cmsmk.setMkac(globalUmUser.getMdac());
		cmsmk.setMkad("启用了编号为[" + mhaa + "]的仓库!");
		cmsmk.setMkae(new Date());
		cmsmk.setMkaf(request.getRemoteAddr());
		cmsmkService.saveCmsmk(cmsmk);
		out.print("success");
		return null;
	}

	/**
	 * 仓库信息
	 * @author dz.wang
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/getAllCmsmh.xhtml")
	public String getAllCmsmh(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mhac = RequestHelper.getString(request, "mhac");
		String ecaa = RequestHelper.getString(request, "ecaa");
		String ecad = RequestHelper.getString(request, "ecad");
		String ecae = RequestHelper.getString(request, "ecae");
		if("null".equals(ecad)) {
			ecad = "";
		}
		List<Cmsmh> list = cmsmhService.getAllCmsmhList(mhac, SysParams.STATUS_NORMAL, ecaa, ecad, ecae);
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String datas = "[";
			for(Cmsmh cmsmh : list) {
				datas += "{\"value\":\""+cmsmh.getMhaa()+"\",\"text\":\""+cmsmh.getMhaa()+"_"+cmsmh.getMhab()+"\"},";
			}
			datas += "]";
			out.print(datas.replace("\"},]", "\"}]"));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}
	/**
	 * 仓库信息
	 * @author dz.wang
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws SysException
	 */
	@RequestMapping(value = "/getCmsmhByMhac.xhtml")
	public String getCmsmhByMhac(HttpServletRequest request, HttpServletResponse response, Model model) throws SysException {
		String mhac = RequestHelper.getString(request, "mhac");
		List<Cmsmh> list = cmsmhService.getCmsmhListByMhac(mhac, SysParams.STATUS_NORMAL);
		response.setContentType("text/xml;charset=utf-8");
		response.setHeader("Pragma", "No-cache");
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String datas = "[";
			for(Cmsmh cmsmh : list) {
				datas += "{\"value\":\""+cmsmh.getMhaa()+"\",\"text\":\""+cmsmh.getMhaa()+"_"+cmsmh.getMhab()+"\"},";
			}
			datas += "]";
			out.print(datas.replace("\"},]", "\"}]"));
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return null;
	}
}
